/************************
Author:Bibhudutta Mishra
Usage: To create Win-Back Lead

*************************/

public class OORWinBackLeadCreate
{
Public Account acc{get; set;}
Public String fname{get; set;}
Public String lname{get; set;}
Public String titlestr{get; set;}

public static ID LocalAdsLeadRecordType = RecordTypeHelper.GetRecordTypeID('Lead', 'Local Advertiser');
public OORWinBackLeadCreate(ApexPages.StandardController controller) {
        acc = (account)controller.getRecord();
        fname = ApexPages.currentPage().getParameters().get('fname');
        lname = ApexPages.currentPage().getParameters().get('lname');
        titlestr = ApexPages.currentPage().getParameters().get('title');
        }

public Pagereference pageredirect()
{
   Lead ld = new Lead();
   ld.firstname=fname;
   ld.lastname=lname;
   if(titlestr!='null'){
       ld.title = titlestr;
   }
   ld.company = acc.name;
   ld.Win_Back_Account__c = acc.id;
   ld.phone = acc.phone;
   ld.recordtypeid = LocalAdsLeadRecordType;
   ld.Win_Back_Lead__c = True;
   ld.email=acc.Contact_Email__c;
   ld.LeadSource = 'Win-Back';
   ld.Website = acc.Website;
   ld.State = acc.State__c;
   ld.city =  acc.City__c;
   ld.street = acc.Business_Address__c;
   ld.County__c = acc.County__c;
   ld.PostalCode = acc.Zip__c;
   try{
   insert ld;  
   }
   catch(exception e){
       system.debug('DML exception'+e);
   }      
   PageReference pageRef = new PageReference('/'+ld.id);
   pageRef.setRedirect(true);
   return pageRef;
   }
   static testMethod void OORWinBackLeadCreateTest() {
       Account acc = new Account();
       acc.name = 'abc';
       acc.phone = '9999999999';
       acc.Primary_Contact__c='ABC';
       acc.Contact_Email__c = 'a@b.com';
       acc.type__c='Win-Back';
       acc.Account_Status__c = 'Prospect';
       insert acc;
   }
}